Partial Class Race

End Class

Partial Class TyranntDataContext

#Region "Player"

    Public Function GeneratePlayer(ByVal username As String) As Boolean
        ' We first need to access the user ID from the Users table
        Dim usr = From user In aspnet_Users _
                  Where user.UserName = username _
                  Select user
        ' now check to see if the user record exists in the Player Table
        If usr.Count <> 1 Then
            Return False
        End If
        Dim q = From player In Players _
                Where player.userID = usr.Single().UserId _
                Select player
        ' if player exists then exit
        If q.Count > 0 Then
            Return True
        End If
        ' Create the player entry
        Dim aPlayer As New Player
        aPlayer.userID = usr.Single.UserId
        aPlayer.creationDate = DateTime.Now
        aPlayer.displayName = usr.Single.UserName
        aPlayer.lastLogin = aPlayer.creationDate
        ' Create a false guid for now, this is only used by the client
        aPlayer.guid = New Guid
        Players.InsertOnSubmit(aPlayer)
        SubmitChanges()
        Return True
    End Function

    Public Function GeneratePlayerGuid(ByVal username As String) As Guid
        ' generate new guid
        Dim g As New Guid

        ' find the player record
        Dim p = From player In Players _
                Where player.aspnet_User.UserName = username _
                Select player
        ' Update player record with new GUID
        p.Single.guid = g
        SubmitChanges()
        Return g
    End Function

    Public Function GetPlayerDisplayName(ByVal username As String) As String
        ' find player
        Dim p = From player In Players _
                Where player.aspnet_User.UserName = username _
                Select player.displayName

        Return p.Single
    End Function

#End Region

#Region "Edit Races"

    Public Function GetRaces() As List(Of Race)
        Dim q = (From race In Races _
                 Order By race.name _
                 Select race).ToList
        Return q
    End Function

    Private Function RaceToListItem(ByVal r As Tyrannt.Race, ByVal selected As Boolean) As SelectListItem
        Dim item As New SelectListItem
        item.Text = r.name
        item.Value = r.ID
        If selected = True Then
            item.Selected = True
        End If
        Return item
    End Function

    Public Function GetRaceID(ByVal raceName As String) As Integer
        Dim q = From race In Races _
                Where race.name = raceName _
                Select race.ID

        Return q.Single
    End Function

#End Region

#Region "Edit Images"

    Public Function GetImage(ByVal url As String) As image
        Dim q = From img In images _
                Where img.url = url _
                Select img

        Return q.Single
    End Function

    Public Function GetImages(ByVal selectedImage As String, ByVal type As String, ByVal tag As String) As List(Of Tyrannt.image)
        Dim q As System.Collections.Generic.List(Of Tyrannt.image)
        If type = "" Then
            q = (From img In images _
                 Order By img.name _
                 Select img).ToList
        ElseIf tag = "" Then
            q = (From img In images _
                 Where img.type = type _
                 Order By img.name _
                 Select img).ToList
        Else
            q = (From img In images _
                 Where img.type = type And img.tag = tag _
                 Order By img.name _
                 Select img).ToList
        End If
        Return q
    End Function

    Public Function GetImageDetails(ByVal type As String, ByVal tag As String) As List(Of imageDetail)
        Dim q = (From img In images _
                 Where img.type = type And img.tag = tag _
                 Order By img.name _
                 Select img).ToList
        Dim lst As New List(Of imageDetail)
        For Each img In q
            Dim item As New imageDetail
            item.ImageName = img.name
            item.ImageURL = img.url
            lst.Add(item)
        Next

        Return lst
    End Function

    Private Function ImageToListItem(ByVal img As Tyrannt.image, ByVal selected As Boolean) As SelectListItem
        Dim item As New SelectListItem
        item.Text = img.name
        item.Value = img.url
        If selected = True Then
            item.Selected = True
        End If
        Return item
    End Function

    Public Function GetImages(ByVal selectedImage As String) As List(Of Tyrannt.image)
        Return GetImages(selectedImage, "", "")
    End Function

    Public Function GetImages(ByVal selectedImage As String, ByVal type As String) As List(Of Tyrannt.image)
        Return GetImages(type, "")
    End Function

    Public Function GetImageID(ByVal imageUrl As String) As Integer
        Dim q = From image In images _
                Where image.url = imageUrl _
                Select image.ID

        Return q.Single
    End Function

    Public Function UpdateImage(ByVal imageName As String, ByVal imageUrl As String, ByVal imageType As String, ByVal imageTag As String) As String
        ' if imageUrl is blank ignore
        If imageUrl = "" Then
            Return "error: No Image URL"
        End If
        ' First check to see if the image exists already
        Dim q = From img In images _
                Where img.url = imageUrl _
                Select img
        If q.Count = 0 Then
            ' it doesnt exist so create new
            Dim img As New image
            img.name = imageName
            img.url = imageUrl
            img.type = imageType
            img.tag = imageTag
            images.InsertOnSubmit(img)
        Else
            ' it does exist so just update
            q.Single.name = imageName
            q.Single.type = imageType
            q.Single.tag = imageTag
        End If
        SubmitChanges()
        Return "ok"
    End Function

    Public Function DeleteImage(ByVal imageName As String) As String
        ' if imageUrl is blank, ignore
        If imageName = "" Then
            Return "Error: Image " + imageName + " does not exist"
        End If

        ' if exists, delete
        Dim q = From img In images _
                Where img.name = imageName _
                Select img

        If q.Count = 1 Then
            images.DeleteAllOnSubmit(q)
            SubmitChanges()
        End If
        Return "ok"
    End Function
#End Region

End Class

